# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libjpeg-turbo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4.2
pkgrel=2
pkgdesc="JPEG image codec with accelerated baseline compression and decompression (mingw-w64)"
arch=('any')
url="http://libjpeg-turbo.virtualgl.org"
license=(custom:'BSD-like')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-nasm" "${MINGW_PACKAGE_PREFIX}-gcc")
provides=("${MINGW_PACKAGE_PREFIX}-libjpeg")
conflicts=("${MINGW_PACKAGE_PREFIX}-libjpeg")
replaces=("${MINGW_PACKAGE_PREFIX}-libjpeg")
options=('staticlibs' 'strip')
source=("https://downloads.sourceforge.net/libjpeg-turbo/${_realname}-${pkgver}.tar.gz"
        "0001-header-compat.mingw.patch"
        "libjpeg-turbo-1.3.1-libmng-compatibility.patch"
        "0002-md5sum-fix.patch")
sha256sums=('521bb5d3043e7ac063ce3026d9a59cc2ab2e9636c655a2515af5f4706122233e'
            '166264f617734b33ac55ec8b70e6636d4e409c0d837667afe158e9d28200e6dd'
            '16336caddc949a8a082f39c218b3289288d144bb3b87f62565ed1b294ff8e526'
            'c22fd95d1796cf8fd31617fd3cee87377f7c0a23f4c509a7f82978b3947933ef')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-header-compat.mingw.patch
  patch -p1 -i ${srcdir}/libjpeg-turbo-1.3.1-libmng-compatibility.patch
  patch -p1 -i ${srcdir}/0002-md5sum-fix.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-jpeg8 \
    --enable-shared \
    --enable-static \
    --mandir=${MINGW_PREFIX}/share/man
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  cp "${srcdir}"/${_realname}-${pkgver}/{jinclude,transupp}.h ${pkgdir}${MINGW_PREFIX}/include/

  # Licenses
  # See http://www.libjpeg-turbo.org/About/License
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 README            "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README"
  install -Dm644 README-turbo.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README-turbo.txt"
  install -Dm644 simd/jsimdext.inc "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/simd/jsimdext.inc"
}
